﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SinsenWeb.Helpers;
using SinsenWeb.Models;

namespace SinsenMVC.Helpers
{
    public static class Helper
    {
        public static string F(DateTime? dateTime)
        {
            if (dateTime.HasValue)
            {
                return F(dateTime.Value);
            }
            return string.Empty;
        }
        public static string F(DateTime dateTime)
        {
            return dateTime.ToString("dd.MM.yy");
        }
        public static string H(string input)
        {
            return new HtmlHelper(null, null).Encode(input);
        }
        public static bool CanEdit
        {
            get
            {
                return true;
            }
        }
        public static bool CanAdmin
        {
            get
            {
                var member = LoginHelper.GetCurrentMember();
                if(member.SecurityRole==SecurityRole.Admin.GetHashCode())
                {
                    return true;
                }
                return false;
            }
        }
        public static bool CanEditMember(Member member)
        {
            return CanAdmin || LoginHelper.GetCurrentMember().Id == member.Id;
        }

        public static string IF(string input, Func<bool> predicate)
        {
            if (predicate())
                return input;

            return string.Empty;
        }
        public static string UNLESS(string input, Func<bool> predicate)
        {
            if (predicate())
                return string.Empty;

            return input;
        }
        public static string IF(string input, bool predicate)
        {
            if (predicate)
                return input;

            return string.Empty;
        }
        public static string UNLESS(string input, bool predicate)
        {
            if (predicate)
                return string.Empty;

            return input;
        }


        public static string Fix(string intro)
        {
            return intro.Replace(@"\r\n", " ");
        }

        public static bool IsLoggedIn
        {
            get
            {
                return HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }
    }
}
